package com.guigu.community.controller.util;

import com.guigu.community.service.OrderService;
import com.guigu.community.service.ProcureService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @Author: janecraft
 * @Date: 2024/11/27
 * @Description: com.guigu.erp.util
 * @Version： 1.0
 */
// 将当前的类交给spring容器进行管理
@Component
public class NumberUtils {

    @Autowired
    private ProcureService procureService;

    @Autowired
    private OrderService orderService;

    /**
     * 生成指定采购单编号
     * 要求：200+当前日期+4位流水号
     * @return
     */
    public String generateDocumentId(String type) {
        StringBuffer buffer = new StringBuffer(type);

        Date date = new Date();

        SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd");
        String temp = format.format(date);
        buffer.append(temp);

        String designId = "";
        switch (type) {
            case "200":
                // 获取最大的物料设计单的编号
                designId = this.procureService.queryProcureMaxId();
                break;
            case "211":
                designId = this.orderService.queryMaxId();
                break;
        }
        String flowNumber = "0001";
        if(designId != null) {
            String y1 = designId.substring(3, 7);
            String y2 = date.getYear()+1900 + "";
            if(y1.equals(y2)) {
                int tmp = Integer.parseInt(designId.substring(11)) + 1;
                flowNumber = String.format("%04d",tmp);
            }
        }
        designId = buffer.append(flowNumber).toString();
        return designId;
    }
}
